package com.android.delivery.activity.fragment

import android.app.Activity
import android.content.Intent
import androidx.recyclerview.widget.RecyclerView
import com.android.delivery.R
import com.android.delivery.activity.siji.WaitBusDetailsActivity
import com.android.delivery.adapter.siji.SJOrderAdapter
import com.android.delivery.api.ApiService
import com.android.delivery.base.BaseListFragmet
import com.android.delivery.callback.DialogCallback
import com.android.delivery.callback.LzyResponse
import com.android.delivery.entity.Transport
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.include_refresh_layout.*

/**
 * 骑手-待接单
 */
class SiJiWaitSendListFragment : BaseListFragmet<Transport>() {

    val TRANSFER_REQUESTCODE = 1

    private val sjOrderAdapter: SJOrderAdapter by lazy {
        SJOrderAdapter(0)
    }

    companion object {
        fun newInstance(): SiJiWaitSendListFragment {
            val fragment = SiJiWaitSendListFragment()
            return fragment
        }
    }

    override fun getEnableRefresh(): Boolean {
        return false
    }

    override fun bindRecyclerView(recLv: RecyclerView?) {
        recLv?.adapter = sjOrderAdapter
        sjOrderAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (view.id == R.id.tv_left) {
                var item = sjOrderAdapter.data.get(position) as Transport
                val intent = Intent(context, WaitBusDetailsActivity::class.java)
                intent.putExtra(com.android.delivery.constant.Constants.ITEM, item)
                startActivityForResult(intent, com.darsh.multipleimageselect.helpers.Constants.REQUEST_CODE)
            }
        }
    }

    override fun loadData(page: Int, pageCount: Int) {
        ApiService.transport(this, 2, object : DialogCallback<LzyResponse<List<Transport>>>() {
            override fun onSuccess(response: Response<LzyResponse<List<Transport>>>?) {
                finishRefreshaLayout()
                onGetDataSuc(response!!.body().data)
            }

            override fun onError(response: Response<LzyResponse<List<Transport>>>?) {
                super.onError(response)
                finishRefreshaLayout()
            }
        })
    }

    override fun getData(): MutableList<Transport> {
        return sjOrderAdapter.data
    }

    override fun bindData(isRefresh: Boolean, list: MutableList<Transport>?) {
        if (isRefresh) {
            sjOrderAdapter.setNewData(list)
        } else {
            sjOrderAdapter.addData(list!!)
        }
    }

    fun setEnableRefresh(enableRefresh: Boolean) {
        if (refreshLayout != null) {
            refreshLayout.setEnableRefresh(false)
        }
    }

    /**
     * 结束下拉刷新
     */
    private fun finishRefreshaLayout() {
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == com.darsh.multipleimageselect.helpers.Constants.REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                onRefresh(refreshLayout)
            }
        }
    }

    fun onAutoRefresh() {
        onRefresh(refreshLayout)
    }
}